package com.jieao.template.mapper;

import com.jieao.template.entity.permissionMgr.PermissionDO;
import com.jieao.template.entity.permissionMgr.PermissionTreeVO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * 权限表(Permission)表数据库访问层
 *
 * @author jzw
 * @since 2020-04-30 09:31:06
 */
public interface PermissionMapper
{
    /**
     * 获取当前登录用户的权限code
     *
     * @param userId 用户的 accountId
     * @return java.util.List<java.lang.String>
     * @author Jzw
     * @date 2019/12/27 15:42
     */
    @Select("SELECT DISTINCT " +
            " a.permission_url  " +
            "FROM " +
            " ( " +
            "SELECT " +
            " p.permission_url  " +
            "FROM " +
            " role_permission rp " +
            " LEFT JOIN permission p ON p.id = rp.permission_id " +
            " LEFT JOIN account_role ar ON ar.role_id = rp.role_id  " +
            " LEFT JOIN role r ON r.id = ar.role_id  " +
            "WHERE " +
            " ar.account_id = #{_parameter}  " +
            " AND p.`enable` = TRUE  " +
            " AND p.permission_url is not null  " +
            " AND rp.deleted = FALSE  " +
            " AND p.deleted = FALSE  " +
            " AND r.deleted = FALSE  " +
            " AND r.enable = true  " +
            " AND ar.deleted = FALSE UNION ALL " +
            "SELECT " +
            " p.permission_url  " +
            "FROM " +
            " account_permission ap " +
            " LEFT JOIN permission p ON p.id = ap.permission_id  " +
            "WHERE " +
            " ap.account_id = #{_parameter}  " +
            " AND p.permission_url is not null  " +
            " AND p.`enable` = TRUE  " +
            " AND ap.deleted = FALSE  " +
            " AND p.deleted = FALSE  " +
            " ) a  ")
    List<String> getAllPermissionByUserId(String userId);

    /**
     * 权限树
     *
     * @return java.util.List<com.northeastern.university.common.model.backstage.PermissionTree>
     * @author Jzw
     * @date 2019/12/17 13:19
     */
    @Select("select id,permission_label,permission_type from permission where parent_id='A' and deleted=false and enable =true order by sort")
    @Results({
            @Result(column = "permission_label", property = "permissionLabel"),
            @Result(column = "permission_type", property = "permissionType"),
            @Result(column = "id", property = "id"),
            @Result(column = "id", property = "pChild",
                    many = @Many(select = "com.jieao.template.mapper.PermissionMapper.getPermissionChildTree", fetchType = FetchType.DEFAULT)
            )
    })
    List<PermissionTreeVO> getPermissionTree();

    /**
     * 子权限树
     *
     * @return java.util.List<com.northeastern.university.common.model.backstage.PermissionTree>
     * @author Jzw
     * @date 2019/12/17 13:19
     */
    @Select("select id,permission_label,permission_type from permission where parent_id=#{_parameter} and deleted=false and enable =true order by sort")
    @Results({
            @Result(column = "permission_label", property = "permissionLabel"),
            @Result(column = "permission_type", property = "permissionType"),
            @Result(column = "id", property = "id"),
            @Result(column = "id", property = "pChild",
                    many = @Many(select = "com.jieao.template.mapper.PermissionMapper.getPermissionChildTree", fetchType = FetchType.DEFAULT)
            )
    })
    List<PermissionTreeVO> getPermissionChildTree(String id);

    /**
     * 停启用权限
     *
     * @param id 权限id
     * @return java.lang.Object
     * @author Jzw
     * @date 2019/12/16 15:22
     */
    @Update("update permission set " +
            "enable=not enable " +
            "where id=#{_parameter}")
    void delMenuOrPermission(String id);

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     * @author jzw
     * @since 2020-04-30 09:31:06
     */
    @Select("select " +
            "id," +
            "permission_label," +
            "permission_code," +
            "permission_url," +
            "display," +
            "parent_id," +
            "enable," +
            "sort," +
            "permission_type," +
            "deleted," +
            "create_time," +
            "create_id," +
            "update_time," +
            "update_id " +
            "from permission where deleted =false and enable =true  and id = #{_parameter}"
    )
    PermissionDO queryById(String id);

    /**
     * 查询分页数据
     *
     * @param params 查询筛选条件
     * @return 对象列表
     * @author jzw
     * @since 2020-04-30 09:31:06
     */
    @Select("<script>select " +
            "id," +
            "permission_label," +
            "permission_code," +
            "permission_url," +
            "parent_id," +
            "enable," +
            "permission_type," +
            "deleted," +
            "create_time," +
            "create_id," +
            "update_time," +
            "update_id " +
            "from permission " +
            "where deleted =false and enable =true  " +
            "<if test=\"permissionLabel != null and permissionLabel != ''\">" +
            "and permission_label = #{permissionLabel} " +
            "</if>" +
            "<if test=\"permissionCode != null and permissionCode != ''\">" +
            "and permission_code = #{permissionCode} " +
            "</if>" +
            "<if test=\"permissionUrl != null and permissionUrl != ''\">" +
            "and permission_url = #{permissionUrl} " +
            "</if>" +
            "<if test=\"parentId != null and parentId != ''\">" +
            "and parent_id = #{parentId} " +
            "</if>" +
            "<if test=\"enable != null\">" +
            "and enable = #{enable} " +
            "</if>" +
            "<if test=\"permissionType != null and permissionType != ''\">" +
            "and permission_type = #{permissionType} " +
            "</if>" +
            "<if test=\"createTime != null\">" +
            "and create_time = #{createTime} " +
            "</if>" +
            "<if test=\"createId != null and createId != ''\">" +
            "and create_id = #{createId} " +
            "</if>" +
            "<if test=\"updateTime != null\">" +
            "and update_time = #{updateTime} " +
            "</if>" +
            "<if test=\"updateId != null and updateId != ''\">" +
            "and update_id = #{updateId} " +
            "</if>" +
            "order by sort</script>"
    )
    List<PermissionDO> queryAllByLimit(PermissionDO params);


    /**
     * 通过实体作为筛选条件查询
     *
     * @param permission 实例对象
     * @return 对象列表
     * @author jzw
     * @since 2020-04-30 09:31:06
     */
    @Select("<script>select " +
            "id," +
            "permission_label," +
            "sort," +
            "permission_code," +
            "permission_url," +
            "parent_id," +
            "enable," +
            "permission_type," +
            "deleted," +
            "create_time," +
            "create_id," +
            "update_time," +
            "update_id " +
            "from permission " +
            "where deleted =false and enable =true  " +
            "<if test=\"permissionLabel != null and permissionLabel != ''\">" +
            "and permission_label = #{permissionLabel} " +
            "</if>" +
            "<if test=\"permissionCode != null and permissionCode != ''\">" +
            "and permission_code = #{permissionCode} " +
            "</if>" +
            "<if test=\"permissionUrl != null and permissionUrl != ''\">" +
            "and permission_url = #{permissionUrl} " +
            "</if>" +
            "<if test=\"parentId != null and parentId != ''\">" +
            "and parent_id = #{parentId} " +
            "</if>" +
            "<if test=\"enable != null\">" +
            "and enable = #{enable} " +
            "</if>" +
            "<if test=\"permissionType != null and permissionType != ''\">" +
            "and permission_type = #{permissionType} " +
            "</if>" +
            "<if test=\"createTime != null\">" +
            "and create_time = #{createTime} " +
            "</if>" +
            "<if test=\"createId != null and createId != ''\">" +
            "and create_id = #{createId} " +
            "</if>" +
            "<if test=\"updateTime != null\">" +
            "and update_time = #{updateTime} " +
            "</if>" +
            "<if test=\"updateId != null and updateId != ''\">" +
            "and update_id = #{updateId} " +
            "</if>" +
            "order by sort </script>"
    )
    List<PermissionDO> queryAll(PermissionDO permission);

    /**
     * 新增数据
     *
     * @param permission 实例对象
     * @return 影响行数
     * @author jzw
     * @since 2020-04-30 09:31:06
     */
    @Insert("insert into permission(id, permission_label,display,sort, permission_code, permission_url, parent_id, enable, permission_type, create_time, create_id, update_time, update_id)" +
            " values (#{id}, #{permissionLabel},#{display},#{sort}, #{permissionCode}, #{permissionUrl}, #{parentId}, #{enable}, #{permissionType}, now(), #{createId}, #{updateTime}, #{updateId})")
    void insert(PermissionDO permission);

    /**
     * 修改数据
     *
     * @param permission 实例对象
     * @return 影响行数
     * @author jzw
     * @since 2020-04-30 09:31:06
     */
    @Update("<script>update permission " +
            "<set>" +
            " <if test=\"permissionLabel != null and permissionLabel != ''\">" +
            " permission_label = #{permissionLabel} , " +
            " </if>" +
            " <if test=\"display != null\">" +
            " display = #{display} , " +
            " </if>" +
            " permission_code = #{permissionCode} , " +
            " permission_url = #{permissionUrl} , " +
            " <if test=\"parentId != null and parentId != ''\">" +
            " parent_id = #{parentId} , " +
            " </if>" +
            " <if test=\"sort != null and sort != ''\">" +
            " sort = #{sort} , " +
            " </if>" +
            " <if test=\"enable != null\">" +
            " enable = #{enable} , " +
            " </if>" +
            " <if test=\"permissionType != null and permissionType != ''\">" +
            " permission_type = #{permissionType} , " +
            " </if>" +
            " <if test=\"createId != null and createId != ''\">" +
            " create_id = #{createId} , " +
            " </if>" +
            " <if test=\"updateTime != null\">" +
            " update_time = now(), " +
            " </if>" +
            " <if test=\"updateId != null and updateId != ''\">" +
            " update_id = #{updateId}  " +
            " </if>" +
            "</set>" +
            "where deleted = false and id = #{id}</script>")
    int update(PermissionDO permission);

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 影响行数
     * @author jzw
     * @since 2020-04-30 09:31:06
     */
    @Delete("update permission set deleted = true where id = #{_parameter}")
    int deleteById(String id);

    /**
     * 通过主键集合批量删除数据
     *
     * @param idList 主键集合
     * @author jzw
     * @since 2020-04-30 09:31:06
     */
    @Delete("<script>" +
            "<foreach separator=\";\" collection=\"list\" item=\"item\" >" +
            "update permission set deleted =true where id =#{item}" +
            "</foreach></script>")
    void batchDeletionByIdList(List<String> idList);


}